<?php
/**
 * Acl class
 *
 * @author Lukas Normantas
 */
class Areal_Acl extends Zend_Acl
{
    /**
     * Sets permissions from config
     *
     * @param <Zend_Config> $config
     */
    public function setConfig(Zend_Config $config)
    {
        $this->deny();
        
        foreach($config as $role_id=>$role_config){
            $parents = $role_config->parents ? explode(' ', $role_config->parents): null;
            if(!$this->hasRole($role_id)){
                $this->addRole(new Zend_Acl_Role($role_id), $parents);
            }
            
            if ($role_config->res) foreach ($role_config->res as $res_config){

                // this is awkwards, but for POC
                list($module_name, $controller_name) = explode(
                    Areal_Acl_Resource::DELIMITER,
                    $res_config->id
                );
                $resource = new Areal_Acl_Resource($module_name, $controller_name);

                if(!$this->has($resource->getResourceId())){
                    $this->add($resource);
                }
                $assert_class_name = $res_config->assert ;
                $assert = $assert_class_name ? new $assert_class_name() : null;
                $this->allow($role_id,
                             $resource->getResourceId(),
                             $res_config->action,
                             $assert );
            }
        }
    }
}